package com.iwuke.dance.util.merchants_connect;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;

/**
 * @author: 魏从响
 * @date: 2025/10/17
 */
public class NetworkCapabilitiesUtil {


    //1. 基础检查：网络是否可用
    //这是最常见的场景，替代你原来的 mActiveNetInfo != null && mActiveNetInfo.isConnected() 逻辑：
//    public static boolean isNetworkAvailable(Context context) {
//        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//        if (cm == null) return false;
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            // Android 6.0+ 推荐方式
//            Network activeNetwork = cm.getActiveNetwork();
//            if (activeNetwork == null) return false;
//
//            NetworkCapabilities capabilities = cm.getNetworkCapabilities(activeNetwork);
//            return capabilities != null &&
//                    capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
//                    capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
//        } else {
//            // 兼容旧版本的回退方案
//            NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
//            return activeNetworkInfo != null && activeNetworkInfo.isConnected();
//        }
//    }


    //2. 判断具体网络类型
    //如果你需要区分 Wi-Fi、移动数据等网络类型：
    public static String getNetworkType(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network activeNetwork = cm.getActiveNetwork();
        if (activeNetwork == null) return "DISCONNECTED";

        NetworkCapabilities capabilities = cm.getNetworkCapabilities(activeNetwork);
        if (capabilities == null) return "UNKNOWN";

        if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
            return "WIFI";
        } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
            return "MOBILE";
        } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
            return "VPN";
        } else {
            return "OTHER";
        }
    }

}
